package com.gmail.blackdog1987.carcare.adapter;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.entity.TCarSale;
import com.gmail.blackdog1987.carcare.util.ModelUtil;

public class CarSaleAdapter extends BaseAdapter {

	List<TCarSale> datas;
	Context mContext;
	ImageLoader imageLoader;
	

	public CarSaleAdapter(Context context, List<TCarSale> datas) {
		this.datas = datas;
		this.mContext = context;
		imageLoader = ImageLoaderFactory.create(mContext);
	};
	
	public void addDatas(List<TCarSale> datas){
		this.datas.addAll(datas);
	}
	
	public void setDatas(List<TCarSale> datas){
		if(this.datas==null){
			this.datas = new ArrayList<TCarSale>();
		}
		this.datas.clear();
		this.datas.addAll(datas);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {

		return datas==null?0:datas.size();
	}

	@Override
	public TCarSale getItem(int position) {
		return datas==null?null:datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		final TCarSale m = getItem(position);
//		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_carsale, null);
			holder = new ViewHolder();
			holder.mHead = (CubeImageView) convertView.findViewById(R.id.carsale_img);
			holder.mTitle = (TextView) convertView.findViewById(R.id.carsale_title);
			holder.mExt = (TextView) convertView.findViewById(R.id.carsale_ext);
			holder.mInfo = (TextView) convertView.findViewById(R.id.carsale_info);
			holder.mPrice = (TextView) convertView.findViewById(R.id.carsale_price);
//			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}
		holder.mTitle.setText(m.getBand() + " " + m.getSeries());
		holder.mExt.setText(ModelUtil.getCarsaleExt(m));
		holder.mInfo.setText(ModelUtil.getCarsaleInfo(m));
		holder.mPrice.setText(m.getPrice() + "");
		
		if(m.headurl!=null&&m.headurl.length()!=0){
			App.loadImg(holder.mHead, imageLoader, m.headurl);
		}
//		convertView.setTag(holder);
		return convertView;
	}

	public class ViewHolder {
		TextView mTitle;
		TextView mExt;
		TextView mInfo;
		TextView mPrice;
		CubeImageView mHead;
	}

}
